<template>
  <div class="order-container">
    <el-card style="text-align: center">
      <!-- 条件筛选tabs -->
      <div class="tabs">
        <filter-tabs
          title="是否发货"
          @changeTab="changeSend"
          :list="isSendList"
        ></filter-tabs>
        <filter-tabs
          title="是否付款"
          style="margin-left: 40px"
          @changeTab="changePay"
          :list="isPayList"
        ></filter-tabs>
      </div>
      <!-- 表格 -->
      <el-table :data="orderList" style="width: 100%" border :style="fontStyle">
        <el-table-column type="index" label="#" align="center">
        </el-table-column>
        <el-table-column prop="order_number" label="订单编号" align="center">
        </el-table-column>
        <el-table-column prop="order_price" label="订单价格" align="center">
        </el-table-column>
        <el-table-column prop="prop" label="是否付款" align="center">
          <template slot-scope="{ row }">
            <el-tag v-if="row.pay_status === '1'" type="success">已付款</el-tag>
            <el-tag v-else type="danger">未付款</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="is_send" label="是否发货" align="center">
        </el-table-column>
        <el-table-column prop="time" label="下单时间" align="center">
        </el-table-column>
        <el-table-column prop="prop" label="操作" align="center">
          <template>
            <el-tooltip
              effect="dark"
              :enterable="false"
              content="修改订单地址"
              placement="bottom"
            >
              <el-button
                type="default"
                size="small"
                :style="btnStyle"
                @click="editAddress"
                icon="el-icon-edit"
              ></el-button>
            </el-tooltip>
            <el-tooltip
              effect="dark"
              :enterable="false"
              content="查看物流进度"
              placement="bottom"
            >
              <el-button
                type="default"
                size="small"
                :style="btnStyle"
                icon="el-icon-location-outline"
                @click="queryLogistics"
              ></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <my-pagination
        :params="params"
        :total="orderListData.total"
        @handleSizeChange="handleSizeChange"
        @handleCurrentChange="handleCurrentChange"
      ></my-pagination>
    </el-card>
    <!-- 修改地址对话框 -->
    <el-dialog
      title="修改地址"
      :visible.sync="editReeaddDialogVisible"
      width="50%"
    >
      <el-form
        :model="address"
        label-width="80px"
        :rules="addressRules"
        ref="addressFormRef"
      >
        <el-form-item label="省市区" prop="addressArr">
          <el-cascader
            v-model="address.addressArr"
            :options="citydata"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="detailAddress">
          <el-input v-model="address.detailAddress"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="editReeaddDialogVisible = false">取 消</el-button>
        <el-button type="default" :style="btnStyle" @click="addressConfirm">
          确 定
        </el-button>
      </div>
    </el-dialog>
    <!-- 物流时间线 -->
    <el-dialog
      title="物流信息"
      :visible.sync="logisticsDialogVisible"
      width="50%"
    >
      <el-timeline style="margin-left: 30px">
        <el-timeline-item
          v-for="(activity, index) in logisticsList"
          :key="index"
          :timestamp="activity.time"
        >
          {{ activity.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import { mapActions, mapState, mapGetters } from 'vuex'
import filterTabs from '../components/filter-tabs'
import logisticsList from './logistics'
import citydata from './citydata'
export default {
  name: 'order',
  data() {
    return {
      // 查询参数
      params: {
        pagenum: 1,
        pagesize: 5,
        // 非必须参数
        pay_status: '', // 1已付款，0未付款
        is_send: '', // 1已发货，0未发货
      },
      logisticsDialogVisible: false, // 物流信息对话框状态
      logisticsList, // 物流信息（接口无法提供）
      citydata, // 城市列表（接口无法提供）
      editReeaddDialogVisible: false, // 修改地址对话框状态
      address: {
        addressArr: [], // 收货地址
        detailAddress: '', // 详细地址
      },
      // 验证规则
      addressRules: {
        addressArr: [
          { required: true, message: '请选择省市区', trigger: 'blur' },
        ],
        detailAddress: [
          { required: true, message: '请填写详细地址', trigger: 'blur' },
        ],
      },
      // 是否发货
      isSendList: [
        { label: '全部', status: '' },
        { label: '已发货', status: '1' },
        { label: '未发货', status: '0' },
      ],
      // 是否付款
      isPayList: [
        { label: '全部', status: '' },
        { label: '已付款', status: '1' },
        { label: '未付款', status: '0' },
      ],
    }
  },
  computed: {
    ...mapState('order', ['orderListData']),
    ...mapGetters('order', ['orderList']),
  },
  methods: {
    ...mapActions('order', ['getOrderList']),
    handleSizeChange(pagesize) {
      this.params.pagesize = pagesize
      this.getOrderList(this.params)
    },
    handleCurrentChange(pagenum) {
      this.params.pagenum = pagenum
      this.getOrderList(this.params)
    },
    // 查询物流
    queryLogistics() {
      this.logisticsDialogVisible = true
    },
    // 修改收货地址
    editAddress() {
      this.address = {}
      this.editReeaddDialogVisible = true
    },
    // 修改地址点击确定的回调
    addressConfirm() {
      this.$refs.addressFormRef.validate(v => {
        if (!v) return
        this.editReeaddDialogVisible = false
        this.$message.success('修改地址成功')
      })
    },
    // 发货条件切换
    changeSend(item) {
      this.params.is_send = item.status
      this.getOrderList(this.params)
    },
    // 付款条件切换
    changePay(item) {
      this.params.pay_status = item.status
      this.getOrderList(this.params)
    },
  },
  components: {
    filterTabs,
  },
  mounted() {
    this.getOrderList(this.params)

    this.$on('handleSizeChange', this.handleSizeChange)
    this.$on('handleCurrentChange', this.handleCurrentChange)

    // 监听tab的切换
    this.$on('changeTab', this.changeSend)
    this.$on('changeTab', this.changePay)
  },
}
</script>

<style lang="scss" scoped>
.el-card {
  text-align: left !important;
  .tabs {
    margin-bottom: 15px;
  }
}
</style>
